


import os 


import h5py    
import numpy as np    
import pdb

import debugpy

import matplotlib.pyplot as plt
from os.path import join, split, exists


import blenderproc as bproc

import numpy as np 
import inspect

def trail_mesh_object():
    demo_obj = "/baai-cwm-1/baai_cwm_ml/public_data/scenes/charging_arm/v2/captured_by_3D_scanner_in_iphone16/download_model/textured_output.obj"
    # demo_blend = "logs/blenderkit/materials/0f46a49a-687d-45e2-a5cd-7bf0ccea983f.blend"
    objs = bproc.loader.load_obj(demo_obj)
    print(len(objs))
    for obj in objs:
        obj.set_location([2, 0, 1])
        obj.set_rotation_euler([np.pi, 0, 0])
        # obj.set_local2world_mat(tmat)
        # obj.apply_T(tmat)

        print(type(obj))

        print(obj.set_cp("my_prop", 42))
        print(obj.get_cp("my_prop"))



def trial_camera_pose():
    fx = 100
    fy = 100
    cx = 256
    cy = 256
    image_width = 512
    image_height = 512

    K = np.array([
        [fx, 0, cx],
        [0, fy, cy],
        [0, 0, 1]
    ])
    bproc.camera.set_intrinsics_from_K_matrix(K, image_width, image_height)

    #* alternative 
    # bproc.camera.set_intrinsics_from_blender_params(lens=focal_length, lens_unit="MILLIMETERS")
    # bproc.camera.set_intrinsics_from_blender_params(lens=field_of_view, lens_unit="FOV")


    # OpenCV -> OpenGL
    # cam2world = bproc.math.change_source_coordinate_frame_of_transformation_matrix(cam2world, ["X", "-Y", "-Z"])


    pass



if __name__ == "__main__":


    out_dir = 'logs'

    """
    
    
    """
    # download_things = 'blenderkit'
    # command = 'blenderproc download %s %s'%(download_things, join(out_dir,download_things))
    # os.system(command)
    


    # {
    # "colors": [<np.uint8: [512, 512, 3]>, <np.uint8: [512, 512, 3]>],
    # "normals": [<np.float32: [512, 512]>, <np.float32: [512, 512]>],
    # "distance": [<np.float32: [512, 512]>, <np.float32: [512, 512]>]
    # }

    

    trail_mesh_object()
    trial_camera_pose()
        
    data = bproc.renderer.render() #* RGB

    # bproc.renderer.enable_distance_output()
    # bproc.renderer.enable_depth_output()
    # bproc.renderer.enable_normals_output()
    # pdb.set_trace()

    print(inspect.signature(bproc.writer.write_hdf5).parameters)
    # bproc.writer.write_hdf5('logs/a.hdf5',{})
        
    print('done')


